﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GabageFactory.Models;
using GabageFactory.Utiliy.Crypt;

namespace GabageFactory.Controllers
{
    [Authorize]
    public class QueryController : BaseController
    {
        StackReposity stackReposity = new StackReposity();

        UserReposity userReposity = new UserReposity();

        StackWaterReposity stackWaterReposity = new StackWaterReposity();

        PoolReposity poolReposity = new PoolReposity();

        StackBackWaterReposity stackBackWaterReposity = new StackBackWaterReposity();

        DikeSettleOffsetReposity dikeSettleOffsetReposity = new DikeSettleOffsetReposity();

        StackArdealiteReposity stackArdealiteReposity = new StackArdealiteReposity();

        TailingpondBackWaterReposity tailingpondBackWaterReposity = new TailingpondBackWaterReposity();

        TailingpondDailyWaterReposity tailingpondDailyWaterReposity = new TailingpondDailyWaterReposity();

        TailingpondDitchReposity tailingpondDitchReposity = new TailingpondDitchReposity();

        TailingpondFilldamReposity tailingpondFilldamReposity = new TailingpondFilldamReposity();

        FactoryWeatherReposity factoryWeatherReposity = new FactoryWeatherReposity();

        

        //
        // GET: /Query/Index
        public ActionResult Index()
        {
            int id = 0;
            int stype = 0;

            long fId = 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ShowStackAndTailingoind(fId, 0, id, stype);

            return View();
        }

        //
        // GET: /Record/StackWater/5
        public ActionResult StackWater(string stackId, int? factoryId, DateTime? date)
        {
            User user = GetCurrentUser();
            ViewData["hasPriviledge"] = (user.RoleId == (int)RoleEnum.Expert);
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 1)
            {
                return RedirectToAction("TailingpondWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);
            DateTime monday = Tools.GetWeeklyDate(now);
            IEnumerable<Return_Water_Pond_Base_Info> pools = poolReposity.ListPools(id);
            int poolNum = pools.Count();
            if (poolNum <= 0)
            {
                poolNum = 1;
            }

            stack_pool_daily_water_info[] poolWaterInfoArray = new stack_pool_daily_water_info[poolNum];
            IEnumerable<stack_pool_daily_water_info> poolWaterInfos = stackWaterReposity.GetDailyPoolWaterInfo(id, now);

            for (int i = 0; i < poolNum; i++)
            {
                if (poolWaterInfos != null && i < poolWaterInfos.Count())
                {
                    poolWaterInfoArray[i] = poolWaterInfos.ElementAt(i);
                }
                else
                {
                    stack_pool_daily_water_info poolDailyWaterInfo = new stack_pool_daily_water_info();
                    poolDailyWaterInfo.stack_id = id;
                    poolDailyWaterInfo.pool_no = i + 1;
                    poolDailyWaterInfo.date = now.Date;
                    poolWaterInfoArray[i] = poolDailyWaterInfo;
                }
                stack_pool_daily_water_info poolWaterInfo = poolWaterInfoArray[i];
                stack_pool_daily_water_info mondayPoolWaterInfo = stackWaterReposity.GetDailyPoolWaterInfo(id, poolWaterInfo.pool_no, monday);
                if (mondayPoolWaterInfo != null)
                {
                    poolWaterInfo.pool_water_capacity = mondayPoolWaterInfo.pool_water_capacity;
                }
            }
            ViewData["poolWaterInfos"] = poolWaterInfoArray;

            stack_daily_water_info waterInfo = stackWaterReposity.GetDailyWaterInfo(id, now);
            if (waterInfo == null)
            {
                waterInfo = new stack_daily_water_info();
                waterInfo.stack_id = id;
                waterInfo.date = now.Date;
            }
            stack_daily_water_info mondayWaterInfo = stackWaterReposity.GetDailyWaterInfo(id, monday);
            if (mondayWaterInfo != null)
            {
                waterInfo.partition1_water_capacity = mondayWaterInfo.partition1_water_capacity;
                waterInfo.partition2_water_capacity = mondayWaterInfo.partition2_water_capacity;
                waterInfo.total_down_ammonia = mondayWaterInfo.total_down_ammonia;
                waterInfo.total_down_fluorin = mondayWaterInfo.total_down_fluorin;
                waterInfo.total_down_nitrogen = mondayWaterInfo.total_down_nitrogen;
                waterInfo.total_down_phosphorus = mondayWaterInfo.total_down_phosphorus;
                waterInfo.total_up_ammonia = mondayWaterInfo.total_up_ammonia;
                waterInfo.total_up_fluorin = mondayWaterInfo.total_up_fluorin;
                waterInfo.total_up_nitrogen = mondayWaterInfo.total_up_nitrogen;
                waterInfo.total_up_phosphorus = mondayWaterInfo.total_up_phosphorus;
            }

            if (waterInfo.tester_id.HasValue)
            {
                ViewData["tester"] = userReposity.GetUserById(waterInfo.tester_id.Value);
            }
            else
            {
                ViewData["tester"] = null;
            }

            return View(waterInfo);
        }

        //
        // GET: /Record/BackWater/5
        public ActionResult BackWater(string stackId, long? factoryId, DateTime? date)
        {
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 1)
            {
                return RedirectToAction("TailingpondWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);

            Stack stack = stackReposity.GetStackById(id);

            if (stack != null)
            {
                ViewData["flowInPic"] = stack.FlowInPic;
            }

            stack_backwater_info backWaterInfo = stackBackWaterReposity.GetDailyBackWaterInfo(id, now);
            if (backWaterInfo == null)
            {
                backWaterInfo = new stack_backwater_info();
                backWaterInfo.stack_id = id;
                backWaterInfo.date = now.Date;
                backWaterInfo.slide_width = 1.2f;
                backWaterInfo.siphon_caliber = 500;
            }
            if (backWaterInfo.tester_id.HasValue)
            {
                ViewData["tester"] = userReposity.GetUserById(backWaterInfo.tester_id.Value);
            }
            else
            {
                ViewData["tester"] = null;
            }

            return View(backWaterInfo);
        }

        //
        // GET: /Record/Settle/5
        public ActionResult Settle(string stackId, long? factoryId, DateTime? date)
        {
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 1)
            {
                return RedirectToAction("TailingpondWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);
            ViewData["originalDate"] = now;

            DateTime monthDate = new DateTime(now.Year, now.Month, 1);

            stack_settle_offset settleOffset = dikeSettleOffsetReposity.GetDikeSettleInfo(id, now);
            if (settleOffset == null)
            {
                settleOffset = new stack_settle_offset();
                settleOffset.stack_id = id;
                settleOffset.date = monthDate;
            }

            IEnumerable<Return_Water_Pond_Base_Info> pools = poolReposity.ListPools(id);
            int poolNum = pools.Count();
            if (poolNum <= 0)
            {
                poolNum = 1;
            }

            stack_settle_offset_pool_info[] offsetPoolInfoArray = new stack_settle_offset_pool_info[poolNum];
            IEnumerable<stack_settle_offset_pool_info> offsetPoolInfos = dikeSettleOffsetReposity.GetMonthSettleOffsetPoolInfo(id, now);

            for (int i = 0; i < poolNum; i++)
            {
                if (offsetPoolInfos != null && i < offsetPoolInfos.Count())
                {
                    offsetPoolInfoArray[i] = offsetPoolInfos.ElementAt(i);
                }
                else
                {
                    stack_settle_offset_pool_info offsetPoolInfo = new stack_settle_offset_pool_info();
                    offsetPoolInfo.stack_id = id;
                    offsetPoolInfo.pool_no = i;
                    offsetPoolInfo.date = monthDate.Date;
                    offsetPoolInfoArray[i] = offsetPoolInfo;
                }
            }
            ViewData["offsetPoolInfos"] = offsetPoolInfoArray;

            if (settleOffset.tester_id.HasValue)
            {
                ViewData["tester"] = userReposity.GetUserById(settleOffset.tester_id.Value);
            }
            else
            {
                ViewData["tester"] = null;
            }

            return View(settleOffset);
        }

        //
        // GET: /Record/Ardealite/5
        public ActionResult Ardealite(string stackId, long? factoryId, DateTime? date)
        {
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 1)
            {
                return RedirectToAction("TailingpondWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);

            stack_ardealite_ditch dailyArdealiteDitchInfo = stackArdealiteReposity.GetDailyArdealiteDitchInfo(id, now);
            if (dailyArdealiteDitchInfo == null)
            {
                dailyArdealiteDitchInfo = new stack_ardealite_ditch();
                dailyArdealiteDitchInfo.stack_id = id;
                dailyArdealiteDitchInfo.date = now.Date;
                dailyArdealiteDitchInfo.dike_downstream_slope_rate = "1:2.5";
            }

            if (dailyArdealiteDitchInfo.tester_id.HasValue)
            {
                ViewData["tester"] = userReposity.GetUserById(dailyArdealiteDitchInfo.tester_id.Value);
            }
            else
            {
                ViewData["tester"] = null;
            }

            return View(dailyArdealiteDitchInfo);
        }

        //
        // GET: /Record/Ditch/5
        public ActionResult Ditch(string stackId, long? factoryId, DateTime? date)
        {
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 1)
            {
                return RedirectToAction("TailingpondWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);
            
            IEnumerable<Runoff_Interception_Ditch_Base_Info> ditches = poolReposity.ListDitches(id);
            List<Runoff_Interception_Ditch_Base_Info> usedDitches = ditches.Where(item => item.El > 0 && item.unused != 1).ToList();
            ViewData["ditchs"] = usedDitches;
            string ditchDesignPic = "";
            Stack stack = stackReposity.GetStackById(id);
            if (stack != null)
            {
                ditchDesignPic = stack.DitchDesignPic;
            }
            ViewData["ditchDesignPic"] = ditchDesignPic;
            StackDailyDitchReposity stackDailyDitchReposity = new StackDailyDitchReposity();
            long testerId = 0;
            string ditchNos = "";
            foreach (Runoff_Interception_Ditch_Base_Info ditch in usedDitches)
            {
                Runoff_Interception_Ditch_daily_info ditchRecord = stackDailyDitchReposity.GetDailyDitchInfo(id, ditch.ditch_no, now);
                if (ditchRecord == null)
                {
                    ditchRecord = new Runoff_Interception_Ditch_daily_info();
                    ditchRecord.stack_id = id;
                    ditchRecord.date = now.Date;
                    ditchRecord.ditch_no = ditch.ditch_no;
                }
                ditchNos += ditch.ditch_no + " ";
                testerId = ditchRecord.tester_id.HasValue ? ditchRecord.tester_id.Value : 0;
                ViewData["dailyDitch" + ditch.ditch_no] = ditchRecord;
            }
            ditchNos = ditchNos.TrimEnd();
            ViewData["stack_id"] = id;
            ViewData["ditchNos"] = ditchNos;
            ViewData["date"] = now.Date;
            if (testerId > 0)
            {
                ViewData["tester"] = userReposity.GetUserById(testerId);
            }
            else
            {
                ViewData["tester"] = null;
            }
            return View();
        }

        //
        // GET: /Record/Ditch/5
        public ActionResult PiezometerWell(string stackId, long? factoryId, DateTime? date)
        {
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 1)
            {
                return RedirectToAction("TailingpondWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);
            ViewData["originalDate"] = now.Date;
            now = new DateTime(now.Year, now.Month, 1);

            PiezometerReposity piezometerReposity = new PiezometerReposity();
            PiezometerRecordReposity piezometerRecordReposity = new PiezometerRecordReposity();
            IEnumerable<Piezometer_well_base_info> wells = piezometerReposity.ListWell(id);
            List<Piezometer_well_monthly_info> wellsRecord = piezometerRecordReposity.GetMonthlyPiezometerWellInfo(id, now) ;
            ViewData["wells"] = wells.ToList();
            ViewData["wellsRecord"] = wellsRecord;
            string piezometerWellPic = "";
            Stack stack = stackReposity.GetStackById(id);
            if (stack != null)
            {
                piezometerWellPic = stack.PiezometerWellPic;
            }
            ViewData["piezometerWellPic"] = piezometerWellPic;

            ViewData["date"] = now.Date;

            int testerId = 0;
            if (wellsRecord != null && wellsRecord.Count > 0)
            {
                testerId = wellsRecord[0].tester_id.HasValue ? wellsRecord[0].tester_id.Value : 0;
            }
            if (testerId > 0)
            {
                ViewData["tester"] = userReposity.GetUserById(testerId);
            }
            else
            {
                ViewData["tester"] = null;
            }
            return View();
        }

        //
        // GET: /Record/Ditch/5
        public ActionResult PiezometerBase(string stackId, long? factoryId, DateTime? date)
        {
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 1)
            {
                return RedirectToAction("TailingpondWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);
            ViewData["originalDate"] = now.Date;
            now = new DateTime(now.Year, now.Month, 1);

            PiezometerReposity piezometerReposity = new PiezometerReposity();
            PiezometerRecordReposity piezometerRecordReposity = new PiezometerRecordReposity();
            IEnumerable<Piezometer_base_info> equaps = piezometerReposity.ListEquaps(id);
            List<Piezometer_monthly_info> equapsRecord = piezometerRecordReposity.GetMonthlyPiezometerBaseInfo(id, now);
            List<Piezometer_base_info> topEquaps = null;
            List<Piezometer_base_info> bottomEquaps = null;
            if (equaps != null && equaps.Count() > 0)
            {
                topEquaps = equaps.Where(item => item.piezometer_id == 1).ToList();
                bottomEquaps = equaps.Where(item => item.piezometer_id == 2).ToList(); ;
            }
            ViewData["topEquaps"] = topEquaps.ToList();
            ViewData["bottomEquaps"] = bottomEquaps.ToList();
            List<Piezometer_monthly_info> topEquapsRecord = null;
            List<Piezometer_monthly_info> bottomEquapsRecord = null;
            if (equapsRecord != null && equapsRecord.Count > 0)
            {
                topEquapsRecord = equapsRecord.Where(item => item.piezometer_id == 1).ToList();
                bottomEquapsRecord = equapsRecord.Where(item => item.piezometer_id == 2).ToList(); ;
            }
            ViewData["topEquapsRecord"] = topEquapsRecord;
            ViewData["bottomEquapsRecord"] = bottomEquapsRecord;
            string piezometerEquapPic = "";
            Stack stack = stackReposity.GetStackById(id);
            if (stack != null)
            {
                piezometerEquapPic = stack.PiezometerEquapPic;
            }
            ViewData["piezometerEquapPic"] = piezometerEquapPic;

            ViewData["date"] = now.Date;
            int testerId = 0;
            if (equapsRecord != null && equapsRecord.Count > 0)
            {
                testerId = equapsRecord[0].tester_id.HasValue ? equapsRecord[0].tester_id.Value : 0;
            }
            if (testerId > 0)
            {
                ViewData["tester"] = userReposity.GetUserById(testerId);
            }
            else
            {
                ViewData["tester"] = null;
            }
            return View();
        }

        //
        // GET: /Record/Ditch/5
        public ActionResult Drain(string stackId, long? factoryId, DateTime? date)
        {
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 1)
            {
                return RedirectToAction("TailingpondWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);
            ViewData["originalDate"] = now.Date;
            now = new DateTime(now.Year, now.Month, 1);

            DrainOutletReposity drainOutletReposity = new DrainOutletReposity();
            DrainRecordReposity drainRecordReposity = new DrainRecordReposity(); 
            IEnumerable<Drain_system_basic_info> drains = drainOutletReposity.ListDrains(id);
            Dictionary<Drain_system_basic_info, List<Drain_system_outlet_info>> drainOutlets = new Dictionary<Drain_system_basic_info, List<Drain_system_outlet_info>>();
            int i = 1;
            int testerId = 0;
            foreach (Drain_system_basic_info drain in drains)
            {
                IEnumerable<Drain_system_outlet_info> outlets = drainOutletReposity.ListDrainOutlets(drain.Stack_id, drain.El);
                List<Drain_system_outlet_info> outletList = new List<Drain_system_outlet_info>();
                foreach (Drain_system_outlet_info outlet in outlets)
                {
                    outletList.Add(outlet);
                }
                drainOutlets.Add(drain, outletList);
                List<Drain_system_Monthly_info> drainRecords = drainRecordReposity.GetMonthlyDrainInfo(id, drain.El, now);
                ViewData["drainRecords" + i] = drainRecords;
                if (drainRecords != null && drainRecords.Count > 0)
                {
                    testerId = drainRecords[0].tester_id.HasValue ? drainRecords[0].tester_id.Value : 0;
                }
                i++;
            }

            ViewData["drainOutlets"] = drainOutlets;

            ViewData["date"] = now.Date;
            if (testerId > 0)
            {
                ViewData["tester"] = userReposity.GetUserById(testerId);
            }
            else
            {
                ViewData["tester"] = null;
            }
            return View();
        }

        //
        // GET: /Record/TailingpondWater/5
        public ActionResult TailingpondWater(string stackId, long? factoryId, DateTime? date)
        {
            User user = GetCurrentUser();
            ViewData["hasPriviledge"] = (user.RoleId == (int)RoleEnum.Expert);
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 0)
            {
                return RedirectToAction("StackWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);
            DateTime monday = Tools.GetWeeklyDate(now);

            tailingpond_daily_water_info dailyWaterInfo = tailingpondDailyWaterReposity.GetDailyWaterInfo(id, now);
            if (dailyWaterInfo == null)
            {
                dailyWaterInfo = new tailingpond_daily_water_info();
                dailyWaterInfo.tailingpond_id = id;
                dailyWaterInfo.date = now.Date;
            }
            tailingpond_daily_water_info mondaydDilyWaterInfo = tailingpondDailyWaterReposity.GetDailyWaterInfo(id, monday);
            if (mondaydDilyWaterInfo != null)
            {
                dailyWaterInfo.pool_water_capacity = mondaydDilyWaterInfo.pool_water_capacity;
                dailyWaterInfo.reservoir_water_capacity = mondaydDilyWaterInfo.reservoir_water_capacity;
            }

            if (dailyWaterInfo.tester_id.HasValue)
            {
                ViewData["tester"] = userReposity.GetUserById(dailyWaterInfo.tester_id.Value);
            }
            else
            {
                ViewData["tester"] = null;
            }
            return View(dailyWaterInfo);
        }

        //
        // GET: /Record/TailingpondBackWater/5
        public ActionResult TailingpondBackWater(string stackId, long? factoryId, DateTime? date)
        {
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 0)
            {
                return RedirectToAction("StackWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);

            tailingpond_backwater_info backWaterInfo = tailingpondBackWaterReposity.GetDailyBackwaterInfo(id, now);
            if (backWaterInfo == null)
            {
                backWaterInfo = new tailingpond_backwater_info();
                backWaterInfo.tailingpond_id = id;
                backWaterInfo.date = now.Date;
            }

            if (backWaterInfo.tester_id.HasValue)
            {
                ViewData["tester"] = userReposity.GetUserById(backWaterInfo.tester_id.Value);
            }
            else
            {
                ViewData["tester"] = null;
            }

            return View(backWaterInfo);
        }

        //
        // GET: /Record/TailingpondDitch/5
        public ActionResult TailingpondDitch(string stackId, long? factoryId, DateTime? date)
        {
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 0)
            {
                return RedirectToAction("StackWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);

            tailingpond_daily_ditch_info ditchInfo = tailingpondDitchReposity.GetDailyDitchInfo(id, now);
            if (ditchInfo == null)
            {
                ditchInfo = new tailingpond_daily_ditch_info();
                ditchInfo.tailingpond_id = id;
                ditchInfo.date = now.Date;
            }
            if (ditchInfo.tester_id.HasValue)
            {
                ViewData["tester"] = userReposity.GetUserById(ditchInfo.tester_id.Value);
            }
            else
            {
                ViewData["tester"] = null;
            }

            return View(ditchInfo);
        }

        //
        // GET: /Record/TailingpondFilldam/5
        public ActionResult TailingpondFilldam(string stackId, long? factoryId, DateTime? date)
        {
            int id = 0;
            int stype = 0;
            ParseSid(stackId, ref id, ref stype);

            if (stype == 0)
            {
                return RedirectToAction("StackWater", new { stackId = stackId, factoryId = factoryId, date = date });
            }

            DateTime now = date.HasValue ? date.Value : DateTime.Now;

            long fId = factoryId.HasValue ? factoryId.Value : 0;
            if (GetCurrentUser().RoleId == (int)RoleEnum.Expert || GetCurrentUser().RoleId == (int)RoleEnum.Analyzer)
            {
                ViewData["showFactory"] = true;
                ShowFactoryList(fId);
            }
            else
            {
                ViewData["showFactory"] = false;
                fId = GetCurrentUser().FactoryId;
            }
            ViewData["factoryId"] = fId;
            ViewData["stackId"] = stackId;
            ShowStackAndTailingoind(fId, 0, id, stype);

            tailingpond_daily_filldam_info filldamInfo = tailingpondFilldamReposity.GetDailyFilldamInfo(id, now);
            
            if (filldamInfo == null)
            {
                filldamInfo = new tailingpond_daily_filldam_info();
                filldamInfo.tailingpond_id = id;
                filldamInfo.date = now.Date;
            }
            if (filldamInfo.tester_id.HasValue)
            {
                ViewData["tester"] = userReposity.GetUserById(filldamInfo.tester_id.Value);
            }
            else
            {
                ViewData["tester"] = null;
            }

            return View(filldamInfo);
        }
    }
}
